package org.marsdonne.commons;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

/**
 * Facade
 */
public class SnowFlake implements IdentifierGenerator {
    private static final SnowFlakeCore SNOW_FLAKE_CORE = new SnowFlakeCore();

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);

        if (id != null && Long.valueOf(id.toString()) > 0) {
            return id;
        } else {
            return SNOW_FLAKE_CORE.nextId();
        }
    }
}
